﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using ChinaDynastyTours.Models;

namespace ChinaDynastyTours.Controls
{
    class LogCtrl
    {
        #region 日志数据

        public static DataTable GetLog(LogTypeEnum type)
        {
            Database db = DatabaseFactory.CreateDatabase();
            string Sql = string.Format(@"SELECT * FROM sys_Log WHERE LogType ='{0}'", type);
            if (type == LogTypeEnum.全部)
                Sql = "SELECT * FROM sys_Log";
            DbCommand dbCommand = db.GetSqlStringCommand(Sql);

            DataSet ds = db.ExecuteDataSet(dbCommand);
            if (ds.Tables.Count > 0)
                return ds.Tables[0];
            else
                return null;
        }

        public static DataTable GetLog(LogTypeEnum type, string startTime, string endTime)
        {
            Database db = DatabaseFactory.CreateDatabase();
            string Sql = string.Format(@"SELECT * FROM sys_Log WHERE LogType ='{0}' AND SendTime>='{1}' AND SendTime<='{2}'", type, startTime, endTime);
            if (type == LogTypeEnum.全部)
                Sql = string.Format(@"SELECT * FROM sys_Log WHERE  SendTime>='{0}' AND SendTime<='{1}'", startTime, endTime);
            DbCommand dbCommand = db.GetSqlStringCommand(Sql);

            DataSet ds = db.ExecuteDataSet(dbCommand);
            if (ds.Tables.Count > 0)
                return ds.Tables[0];
            else
                return null;
        }

        public static void DeleteClassLog(string type)
        {
            Database db = DatabaseFactory.CreateDatabase();
            string Sql = string.Format(@"DELETE  FROM sys_Log WHERE LogType ='{0}'", type);
            DbCommand dbCommand = db.GetSqlStringCommand(Sql);

            db.ExecuteDataSet(dbCommand);
        }

        public static void DeleteLog(string logId)
        {
            Database db = DatabaseFactory.CreateDatabase();
            string Sql = string.Format(@"DELETE FROM sys_Log WHERE LogID ='{0}'", logId);
            DbCommand dbCommand = db.GetSqlStringCommand(Sql);

            db.ExecuteDataSet(dbCommand);
        }

        public static void InsertLog(LogEntity entity)
        {
            Database db = DatabaseFactory.CreateDatabase();
            string Sql = string.Format(@"INSERT INTO [sys_Log]([LogType], [TourgroupID], [TourgroupName], [UserName], [RealName], [Title], [SendTime], [Success],[Descript],[Recipients])
                                                     VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}',{7},'{8}','{9}')",
                                                     entity.LogType,
                                                     entity.TourgroupID,
                                                     entity.TourgroupName,
                                                     entity.UserName,
                                                     entity.RealName,
                                                     entity.Title,
                                                     entity.SendTime,
                                                     entity.Success,
                                                     entity.Descript,
                                                     entity.Recipients);
            DbCommand dbCommand = db.GetSqlStringCommand(Sql);

            db.ExecuteDataSet(dbCommand);
        }
        #endregion

    }
}
